package com.beetlestech.nfe.main.biz.com.impl;
import java.util.ArrayList;
import java.util.List;
import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.util.CollectionUtil;
import com.beetlestech.nfe.main.biz.com.HbVAdminLogin;
import com.beetlestech.nfe.main.biz.com.dto.UserInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.tbl.MstusrDTO;
import com.beetlestech.nfe.main.db.view.VadminlogininfoDTO;
public class HbVAdminLoginImpl extends AbstractDBLogic implements HbVAdminLogin, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	/**
	 * 職員情報データ
	 */
	protected static final String SH_INFO = "職員情報データ";

	/**
	 * APPL_ID:COM
	 */
	protected static final String COM = "COM";

	/**
	 * FUNCTION_ID:ListMaxCount
	 */
	protected static final String LIST_MAX_COUNT = "ListMaxCount";

	/**
	 * ITEM_ID:StaffList
	 */
	protected static final String STAFF_LIST = "StaffList";

	/**
	 * ITEM_ID:AgentList
	 */
	protected static final String AGENT_LIST = "AgentList";

	/**
	 * FUNCTION_ID:Mode
	 */
	protected static final String FUNCTION_ID_MODE = "Mode";

	/**
	 * ITEM_ID:AuthenticationCondition
	 */
	protected static final String ITEM_ID_AUTHENTICATION_CONDITION = "AuthenticationCondition";

	/**
	 * ID種別:0
	 */
	protected static final String ID_TYPE_0 = "0";

	/**
	 * ID種別:1
	 */
	protected static final String ID_TYPE_1 = "1";

	/**
	 * ID種別:2
	 */
	protected static final String ID_TYPE_2 = "2";

	/**
	 * 表示順
	 */
	protected static final String COM_KEY3 = "comKey3";

	
	@Override
	public UserInfoDTO getLoginAdminInfo(
			String loginName, String password) {
		/*MstusrDTO userDTO = new MstusrDTO();
		userDTO.setusername(loginName);
		userDTO.setphone(loginName);
		userDTO.setemail(loginName);*/
		VadminlogininfoDTO vadminlogininfoDTO =new VadminlogininfoDTO();
		vadminlogininfoDTO.setusername(loginName);
		//List<MstusrDTO> userDTOs = queryDAO.queryForObjectList(SQLID.LOGIN_USRE_EQUAL_SEARCH, userDTO);
		List<VadminlogininfoDTO> adminDTOs=queryDAO.queryForObjectList(VadminlogininfoDTO.EQUAL_SEARCH, vadminlogininfoDTO);
		UserInfoDTO adminLoginInfo = null;
		if (CollectionUtil.isEmpty(adminDTOs)) {
			//検出件数が0件の場合
			createApplicationException(M00010WC, SH_INFO);
		} else {
			//処理１で2件以上取得できた場合は、「ApplicationException」を返す
			if(convertToVAdminListInfo(adminDTOs).size()>1){
               createApplicationException(M10190WS, SH_INFO);
			}
			else{
			    adminLoginInfo =convertToVAdminListInfo(adminDTOs).get(0);
				if(null != password && "" != password && ! password.equals(adminLoginInfo.getpassword())){
					createApplicationException(M00010WC, SH_INFO);
				}
			}
		} /*else {
			userInfo = setUserInfo(userDTOs.get(0));
			if (null != password && "" != password && ! password.equals(userInfo.getpassword())) {
				createApplicationException(M00010WC, SH_INFO);
			}
		}*/

		//検出データを返す
		
		return adminLoginInfo;
	}
	private List<UserInfoDTO> convertToVAdminListInfo(List<VadminlogininfoDTO> itemArray){
		List<UserInfoDTO> vadminloginlistInfoDTOArray = new ArrayList<UserInfoDTO>();
		int a=-1;
		UserInfoDTO temp = null;
		for(VadminlogininfoDTO item:itemArray){
			if(item.getusrid()==a){
				//temp.setcatagorycname(temp.getcatagorycname()+","+item.getcatagorycname());
				List<Integer> jurisdictionidarray=temp.getJurisdictionidarray();
				jurisdictionidarray.add(item.getjurisdictionid());
				temp.setJurisdictionidarray(jurisdictionidarray);
				setManage(item.getjurisdictionid(),temp);
			}else{
				if(temp != null){
					vadminloginlistInfoDTOArray.add(temp);
				}
				temp = new UserInfoDTO();
				temp.setusrid(item.getusrid());
				temp.setusername(item.getusername());
				temp.settype(item.gettype());
				temp.setstate(item.getstate());
				temp.setPhone(item.getphone());
				temp.setpassword(item.getpassword());
				List<Integer> jurisdictionidArray=new ArrayList<Integer>();
				setManage(item.getjurisdictionid(),temp);
				jurisdictionidArray.add(item.getjurisdictionid());
				temp.setJurisdictionidarray(jurisdictionidArray);
				temp.setfirstloginflg(item.getfirstloginflg());
				temp.setEmail(item.getemail());
				a=item.getusrid();
			}
		}
		if(temp != null){
			vadminloginlistInfoDTOArray.add(temp);;
		}
		return vadminloginlistInfoDTOArray;
	}
	
	/*private UserInfoDTO setUserInfo(MstusrDTO mstusrDTO){
		UserInfoDTO userInfo = new UserInfoDTO();
		userInfo.setfirstloginflg(mstusrDTO.getfirstloginflg());
		userInfo.setpassword(mstusrDTO.getpassword());
		userInfo.setstate(mstusrDTO.getstate());
		userInfo.settype(mstusrDTO.gettype());
		userInfo.setusername(mstusrDTO.getusername());
		userInfo.setusrid(mstusrDTO.getusrid());
		userInfo.setEmail(mstusrDTO.getemail());
		userInfo.setPhone(mstusrDTO.getphone());
		
		return userInfo;
	}*/
	
	public void UpdatePassword(Integer usr_id, String oldPassword, String password) {
		MstusrDTO userDTO = null;
		
		if (oldPassword != null && oldPassword.equals("")) {
			userDTO = new MstusrDTO();
			userDTO.setusrid(usr_id);
			userDTO.setpassword(oldPassword);
			List<MstusrDTO> userDTOs = queryDAO.queryForObjectList(MstusrDTO.EQUAL_SEARCH, userDTO);
			if (userDTOs.size() == 0) {
				createApplicationException(M10540WS);
			}
		}
		
		userDTO = new MstusrDTO();
		userDTO.setusrid(usr_id);
		userDTO.setpassword(password);
		
		updateDAO.updateOne(MstusrDTO.UPDATE, userDTO, M10190WS, SH_INFO);
	}
	private void setManage(int manage,UserInfoDTO temp){
		switch (manage) {
		case 1:
			temp.setIsSystemManage(true);
			break;
		case 2:
			temp.setIsStudentManage(true);;
			break;
		case 3:
			temp.setIsCourseManage(true);;
			break;
		case 4:
			temp.setIsExamQuestionManage(true);
			break;
		case 5:
			temp.setIsHomeWorkManage(true);
			break;
		case 6:
			temp.setIsTeacherManage(true);
			break;
		default:
			break;
		}
		
	}
}
